package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.common.utils.Common;
import com.think.teaching.model.Asset;
import com.think.teaching.service.impl.AssetServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 资源 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags =  "资源接口文档")
public class AssetController extends BaseController{
	
	
	private final AssetServiceImpl assetService;

	public AssetController(AssetServiceImpl assetService) {
		this.assetService = assetService;
	}


	@ApiOperation(value = "管理员新增资源")
	@RequestMapping(value = "admin/asset",method = {RequestMethod.POST})
	@ResponseBody
	public Result AddAssets(@RequestBody()Asset data) {
		Result result = data.CheckObjectComplete();
		if (result.IsSuccess()) {
			if (assetService.InsertNewAsset(data) !=null) {
				result = Result.success(data);
			}
			else
			result = Result.failure(ResultCode.SYSTEM_INNER_ERROR);

		}
		return result;
		
	}
	
	@ApiOperation(value = "管理员删除资源")
	@RequestMapping(value = "admin/asset/{id}",method = {RequestMethod.DELETE})
	public Result DeleteAssets(@PathVariable("id")Integer id){
		if (assetService.DeleteObjectById(id)) {
			return  Result.success();
		}
		return Result.failure(ResultCode.ASSET_NOT_EXITS);

	}
	
	@ApiOperation(value = "管理员设置资源状态")
	@RequestMapping(value = "admin/asset/{id}/state",method = {RequestMethod.PUT})
	public Result SetAssetsState(@PathVariable("id") Integer id ,
								 @RequestParam("state") Integer state){
		Asset asset = assetService.getById(id);
		if (asset == null) {
			return Result.failure(ResultCode.ASSET_NOT_EXITS);
		}
		else {
			asset.setState(state);
			if (assetService.updateById(asset)) {
				return Result.success(asset);
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);

		}
	}
	
	@ApiOperation(value = "管理员获取资源列表")
	@RequestMapping(value = "admin/assets/{page}/{count}",method = {RequestMethod.GET})
	public Result AdminSelectAllAssets(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("type")Integer type)
	{
		return Result.success(assetService.ListAssetWithCreatorAndType(Common.GetCurrentAuthor().getUserId(),type,page,count,false));
	}
	

	
	@ApiOperation(value = "用户获取资源列表")
	@RequestMapping(value = "user/assets/{page}/{count}",method = {RequestMethod.GET})
	public Result UserSelectAllAssets(@PathVariable("page")Integer page,@PathVariable("count")Integer count,@RequestParam("type")Integer type)
	{
		return Result.success(assetService.ListAssetWithCreatorAndType(Common.GetCurrentAuthor().getUserId(),type,page,count,true));
	}
	
//	@ApiOperation(value = "用户获取根据id获取资源")
//	@RequestMapping(value = "user/assets/{id}",method = {RequestMethod.GET})
//	public Result UserSelectAssets(@PathVariable("id")Integer id)
//	{
//		Asset asset = assetService.SelectAsset(id);
//		if (asset == null) {
//			return Result.failure(ResultCode.ASSET_NOT_EXITS);
//		}
//		else {
//			return Result.success(asset);
//
//		}	
//	}
}

